package com.wxdc.nmgyt.nmgytwydc.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.wxdc.nmgyt.nmgytwydc.app.MyApp;

/**
 * Created by Administrator on 2018/4/24.
 * SharedPreferences是Android平台上一个轻量级的存储类，用来保存应用的一些常用配置
 */

public class SharedPreferenceUtil {
    private static final String TOKEN = "token";
    private static final String USERNAME = "user_name";
    private static final String PASSWORD = "user_pwd";
    private static final String HTTPURL = "http_url";
    private static final String SID = "sid";//tokenid
    private static final String USERREALNAME = "realname";//用户真实名称
    private static final String USERWORKUNIT = "workunit";//用户所在单位
    private static final String GZDWDM = "GZDWDM";//工作单位代码
    private static final String GZDWMC = "GZDWMC";//工作单位名称
    //以XML文件的形式保存在 /data/data/PACKAGE_NAME/shared_prefs目录下(nmgyt_wydc_sp.xml)
    private static final String SHAREDPREFERENCES_NAME = "nmgyt_wydc_sp";//自定义名称

    private volatile static  SharedPreferences sPreferences;

    public static SharedPreferences getAppSp() {
        if (sPreferences == null) {
            synchronized (SharedPreferences.class) {
                if (sPreferences == null) {
                    sPreferences = MyApp.getInstance().getSharedPreferences(SHAREDPREFERENCES_NAME, Context.MODE_PRIVATE);
                }
            }
        }
        return sPreferences;
    }


    public static String getUsername() {
        return getAppSp().getString(USERNAME, "");
    }

    public static void setUsername(String username) {
        getAppSp().edit().putString(USERNAME, username).apply();
    }

    public static String getPassword() {
        return getAppSp().getString(PASSWORD, "");
    }

    public static void setPassword(String password) {
        getAppSp().edit().putString(PASSWORD, password).apply();
    }

    public static String getToken() {
        return getAppSp().getString(TOKEN, "");
    }

    public static void setToken(String id) {
        getAppSp().edit().putString(TOKEN, id).apply();
    }

    public static String getHttp() {
        return getAppSp().getString(HTTPURL, "");
    }

    public static void setHttp(String httpUrl) {
        getAppSp().edit().putString(HTTPURL, httpUrl).apply();
    }

    public static String getSid(){
        return getAppSp().getString(SID, "");
    }
    public static void setSid(String sid){
        getAppSp().edit().putString(SID, sid).apply();
    }

    public static String getUserrealname(){
        return getAppSp().getString(USERREALNAME, "");
    }
    public static void setUserrealname(String realname){
        getAppSp().edit().putString(USERREALNAME, realname).apply();
    }
    public static String getUserworkunit(){
        return getAppSp().getString(USERWORKUNIT, "");
    }
    public static void setUserworkunit(String workunit){
        getAppSp().edit().putString(USERWORKUNIT, workunit).apply();
    }
    public static String getGzdwdm(){
        return getAppSp().getString(GZDWDM, "");
    }
    public static void setGzdwdm(String gzdmdm){
        getAppSp().edit().putString(GZDWDM,gzdmdm).apply();
    }
    public static String getGzdwmc(){
        return getAppSp().getString(GZDWMC, "");
    }
    public static void setGzdwmc(String gzdwmc){
        getAppSp().edit().putString(GZDWMC,gzdwmc).apply();
    }
}
